#include "lcd_init.h"
#include "../lcd_drive.h"
#include "bflb_mtimer.h"

void LCD_GPIO_Init(void)
{
	Init_SPI();
}

/******************************************************************************
	  函数说明：LCD串行数据写入函数
	  入口数据：dat  要写入的串行数据
	  返回值：  无
******************************************************************************/
void LCD_Writ_Bus(uint8_t dat)
{
	LCD_CS_Clr();
	bflb_spi_poll_test(dat);
	LCD_CS_Set();
}

/******************************************************************************
	  函数说明：LCD写入数据
	  入口数据：dat 写入的数据
	  返回值：  无
******************************************************************************/
void LCD_WR_DATA8(uint8_t dat)
{
	LCD_Writ_Bus(dat);
}
/******************************************************************************
	  函数说明：LCD写入数据
	  入口数据：dat 写入的数据
	  返回值：  无
******************************************************************************/
void LCD_WR_DATA(uint32_t dat)
{
	LCD_Writ_Bus(dat >> 16);
	LCD_Writ_Bus(dat >> 8);
	LCD_Writ_Bus(dat);
}

/******************************************************************************
	  函数说明：LCD写入命令
	  入口数据：dat 写入的命令
	  返回值：  无
******************************************************************************/
void LCD_WR_REG(uint8_t dat)
{
	LCD_DC_Clr();
	LCD_Writ_Bus(dat);
	LCD_DC_Set();
}
/******************************************************************************
	  函数说明：设置起始和结束地址
	  入口数据：x1,x2 设置列的起始和结束地址
				y1,y2 设置行的起始和结束地址
	  返回值：  无
******************************************************************************/
void LCD_Address_Set(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
	LCD_WR_REG(0x2a);
	LCD_WR_DATA8(x1 >> 8);
	LCD_WR_DATA8(x1);
	LCD_WR_DATA8(x2 >> 8);
	LCD_WR_DATA8(x2);
	LCD_WR_REG(0x2b);
	LCD_WR_DATA8(y1 >> 8);
	LCD_WR_DATA8(y1);
	LCD_WR_DATA8(y2 >> 8);
	LCD_WR_DATA8(y2);
	LCD_WR_REG(0x2c);
}

void LCD_Init(void)
{
	LCD_GPIO_Init();
	LCD_RES_Clr();
	bflb_mtimer_delay_ms(100);
	LCD_RES_Set();
	bflb_mtimer_delay_ms(100);
	LCD_BLK_Set();
	bflb_mtimer_delay_ms(100);
	LCD_WR_REG(0xE0);
	LCD_WR_DATA8(0x00);
	LCD_WR_DATA8(0x07);
	LCD_WR_DATA8(0x0f);
	LCD_WR_DATA8(0x0D);
	LCD_WR_DATA8(0x1B);
	LCD_WR_DATA8(0x0A);
	LCD_WR_DATA8(0x3c);
	LCD_WR_DATA8(0x78);
	LCD_WR_DATA8(0x4A);
	LCD_WR_DATA8(0x07);
	LCD_WR_DATA8(0x0E);
	LCD_WR_DATA8(0x09);
	LCD_WR_DATA8(0x1B);
	LCD_WR_DATA8(0x1e);
	LCD_WR_DATA8(0x0f);

	LCD_WR_REG(0xE1);
	LCD_WR_DATA8(0x00);
	LCD_WR_DATA8(0x22);
	LCD_WR_DATA8(0x24);
	LCD_WR_DATA8(0x06);
	LCD_WR_DATA8(0x12);
	LCD_WR_DATA8(0x07);
	LCD_WR_DATA8(0x36);
	LCD_WR_DATA8(0x47);
	LCD_WR_DATA8(0x47);
	LCD_WR_DATA8(0x06);
	LCD_WR_DATA8(0x0a);
	LCD_WR_DATA8(0x07);
	LCD_WR_DATA8(0x30);
	LCD_WR_DATA8(0x37);
	LCD_WR_DATA8(0x0f);

	LCD_WR_REG(0xC0);
	LCD_WR_DATA8(0x10);
	LCD_WR_DATA8(0x10);

	LCD_WR_REG(0xC1);
	LCD_WR_DATA8(0x41);

	LCD_WR_REG(0xC5);
	LCD_WR_DATA8(0x00);
	LCD_WR_DATA8(0x22);
	LCD_WR_DATA8(0x80);

	LCD_WR_REG(0x36);
	if (USE_HORIZONTAL == 0)
		LCD_WR_DATA8(0x48);
	else if (USE_HORIZONTAL == 1)
		LCD_WR_DATA8(0x88);
	else if (USE_HORIZONTAL == 2)
		LCD_WR_DATA8(0x28);
	else
		LCD_WR_DATA8(0xE8);

	LCD_WR_REG(0x3A);
	LCD_WR_DATA8(0x66);

	LCD_WR_REG(0XB0);
	LCD_WR_DATA8(0x00);
	LCD_WR_REG(0xB1);
	LCD_WR_DATA8(0xB0);
	LCD_WR_DATA8(0x11);
	LCD_WR_REG(0xB4);
	LCD_WR_DATA8(0x02);
	LCD_WR_REG(0xB6);
	LCD_WR_DATA8(0x02);
	LCD_WR_DATA8(0x02);

	LCD_WR_REG(0xB7);
	LCD_WR_DATA8(0xC6);
	LCD_WR_REG(0xE9);
	LCD_WR_DATA8(0x00);

	LCD_WR_REG(0XF7);
	LCD_WR_DATA8(0xA9);
	LCD_WR_DATA8(0x51);
	LCD_WR_DATA8(0x2C);
	LCD_WR_DATA8(0x82);

	LCD_WR_REG(0x11);
	bflb_mtimer_delay_ms(120);
	LCD_WR_REG(0x29);
}
